/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.service.catalogue.ws;

import java.util.UUID;
import javax.inject.Inject;
import javax.jws.WebService;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.CamelExecutionException;
import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.cdi.CdiCamelContext;
import org.apache.cxf.annotations.SchemaValidation;
import riv.service.catalogue._1.OfferType;
import riv.service.catalogue._1.ResultCodeEnum;
import riv.service.catalogue._1.ResultLevelEnum;
import riv.service.catalogue._1.ResultType;
import se.riv.service.catalogue.pulloffers.v1.rivtabp21.PullOffersResponderInterface;
import se.riv.service.catalogue.pulloffersresponder.v1.PullOffersResponseType;
import se.riv.service.catalogue.pulloffersresponder.v1.PullOffersType;
import se.sll.service.catalogue.composite.helper.service.HsaCache;
import se.sll.service.catalogue.common.rest.response.HsaData;

/**
 *
 * @author khaleddaham
 */
@Slf4j
@WebService(
        endpointInterface = "se.riv.service.catalogue.pulloffers.v1.rivtabp21.PullOffersResponderInterface",
        serviceName = "PullOffers")
@SchemaValidation
public class PullOffers implements PullOffersResponderInterface {

    @Inject
    private CdiCamelContext camelCtx;
    @Inject
    private HsaCache hsaCache;

    /**
     *
     * @param string
     * @param request
     * @return
     */
    @Override
    public PullOffersResponseType pullOffers(String string, PullOffersType request) {
        UUID logId = UUID.randomUUID();
        log.info("logId: {}, request: {}", logId.toString(), request.toString());
        PullOffersResponseType response = new PullOffersResponseType();
        response.setResult(new ResultType());
        try {
            ProducerTemplate producer = camelCtx.createProducerTemplate();
            response = (PullOffersResponseType) producer.sendBody("direct:start:offers", ExchangePattern.InOut, "");
            // @TODO move this to a processor.
            for (OfferType o : response.getOffer()) {
                HsaData hsa = hsaCache.getHsa(o.getOrganizationalUnit().getHSAId());
                if (hsa == null) {
                    continue;
                }
                try {
                    o.getOrganizationalUnit().setOrganizationName(hsa.getRelativeDistinguishedName());
                    o.getOrganizationalUnit().setPostalAddress(hsa.getPostalAddress().get(0));
                    // careUnit.setPostalCode(hsa.getPostalAddress().get(0));
                    o.getOrganizationalUnit().setStreetAddress(hsa.getStreet());
                } catch (Exception ex) {
                    log.error(ex.getMessage(), ex);
                }
            }
            log.debug("Found {} offers.", response.getOffer().size());
        } catch (CamelExecutionException ex) {
            log.error(ex.getMessage(), ex);
            response.getResult().setCode(ResultCodeEnum.TECHNICAL_ERROR);
            response.getResult().setLevel(ResultLevelEnum.ERROR);
            response.getResult().setMessage(ex.getMessage());
            return response;
        }
        response.getResult().setCode(ResultCodeEnum.TECHNICAL_ERROR);
        response.getResult().setLogId(logId.toString());
        return response;
    }
}